package com.sztx.pay.center.rpc.service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.berbon.user.facade.NewpayUsersFacade;
import com.berbon.util.MD5.MD5Util;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.common.yhbf.YHBFUtil;
import com.sztx.pay.center.rpc.api.domain.B2CRequest;
import com.sztx.pay.center.rpc.api.domain.B2CWithPwdRequest;
import com.sztx.pay.center.rpc.api.domain.ChargeRequest;
import com.sztx.pay.center.rpc.api.domain.ConfirmOrder;
import com.sztx.pay.center.rpc.api.domain.RefundRequest;
import com.sztx.pay.center.rpc.api.domain.ResendQuickVCRequest;
import com.sztx.pay.center.rpc.api.domain.TradeResponse;
import com.sztx.pay.center.rpc.api.domain.TransferRequest;
import com.sztx.pay.center.rpc.api.domain.VerifyQuickPayRequest;
import com.sztx.pay.center.rpc.api.domain.WithdrawRequest;
import com.sztx.pay.center.rpc.api.service.TradeRpcService;
import com.sztx.pay.center.rpc.component.ResendQuickVCService;
import com.sztx.pay.center.rpc.component.TradeB2CService;
import com.sztx.pay.center.rpc.component.TradeChargeService;
import com.sztx.pay.center.rpc.component.TradeConfirmOrderService;
import com.sztx.pay.center.rpc.component.TradeRefundService;
import com.sztx.pay.center.rpc.component.TradeTransferSrervice;
import com.sztx.pay.center.rpc.component.TradeWithdrawService;
import com.sztx.pay.center.rpc.component.VerifyQuickPayService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.common.util.StringUtil;
import com.sztx.se.core.context.SpringContextHolder;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;

@Service
public class TradeRpcServiceImpl implements TradeRpcService{
	@Autowired
	private DubboClientFactory dubboConsumerFactory;
	

	@Override
	public TradeResponse b2c(B2CRequest request) {
		
		TradeB2CService service = SpringContextHolder.applicationContext.getBean("tradeB2CService", TradeB2CService.class);
		return service.executeTade(request);
	}
	@Override
	public TradeResponse b2cwithpwd(B2CWithPwdRequest request) {
		String bankId = request.getBankId();
		String bindNo = request.getBindNo();
		String b2cPayPwd = request.getB2cPayPwd();
		String payerUserId = request.getPayerUserId();
		ValidateUtil.isNotBlank(bankId, "bankId不能为空");
		//余额支付  或者快捷支付验证密码
		if(bankId.equals(Constance.BERBON_PAY_BANKID) || !StringUtil.isBlank(bindNo)){
			ValidateUtil.isNotBlank(b2cPayPwd, "b2cPayPwd不能为空");
			checkPayPwd(payerUserId, b2cPayPwd, 1);
		}
		
		TradeB2CService service = SpringContextHolder.applicationContext.getBean("tradeB2CService", TradeB2CService.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse charge(ChargeRequest request) {
		TradeChargeService service = SpringContextHolder.applicationContext.getBean("tradeChargeService", TradeChargeService.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse withdraw(WithdrawRequest request) {
		TradeWithdrawService service = SpringContextHolder.applicationContext.getBean("tradeWithdrawService", TradeWithdrawService.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse transfer(TransferRequest request) {
		TradeTransferSrervice service = SpringContextHolder.applicationContext.getBean("tradeTransferSrervice", TradeTransferSrervice.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse confirmOrder(ConfirmOrder request) {
		TradeConfirmOrderService service = SpringContextHolder.applicationContext.getBean("tradeConfirmOrderService", TradeConfirmOrderService.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse refund(RefundRequest request) {
		TradeRefundService service = SpringContextHolder.applicationContext.getBean("tradeRefundService", TradeRefundService.class);
		return service.executeTade(request);
	}

	@Override
	public TradeResponse verifyQuickPay(VerifyQuickPayRequest request) {
		VerifyQuickPayService	service = SpringContextHolder.applicationContext.getBean("verifyQuickPayService", VerifyQuickPayService.class);
		return service.executeTade(request);
	}
	@Override
	public TradeResponse resendQuickVC(ResendQuickVCRequest request) {
		ResendQuickVCService service = SpringContextHolder.applicationContext.getBean("resendQuickVCService", ResendQuickVCService.class);
		return service.executeTade(request);
	}

	

	private void checkPayPwd(String userId,String b2cPayPwd,int encType){
		NewpayUsersFacade newpayUsersFacade = 
				dubboConsumerFactory.getDubboClient("newpayUsersFacade");
		List<Map<String, Object>> users = newpayUsersFacade.getUser(userId);
		try {
			b2cPayPwd = YHBFUtil.decryptEncode(b2cPayPwd, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException("密码解密失败,请重试");
		}
		
		
		String salt = b2cPayPwd+users.get(0).get("salt");
		String md5pwd = MD5Util.encrypt(salt, "UTF-8");
		//md5pwd = MD5Util.encrypt(md5pwd+disturse, "UTF-8");
		if(!newpayUsersFacade.checkUsersPwd(userId, md5pwd, null, encType)){
			throw new BusinessException("支付密码错误,请重新输入");
		}
		
	}
	
	

}
